# Maintainer: Anatol Pomozov <anatol.pomozov@gmail.com>
# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

pkgname='rubygems'
pkgver=3.3.25
pkgrel=1
pkgdesc='Package management framework for Ruby'
arch=('any')
url='https://rubygems.org/'
license=('MIT')
depends=('ruby')
#checkdepends=('cmake' 'git' 'ruby-rake' 'ruby-webrick')
backup=('etc/gemrc')
options=('!emptydirs')
source=("https://github.com/rubygems/rubygems/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
        "${pkgname}_stop_so_duplication.patch"
        'gemrc')
sha512sums=('7acfcd0b34ee522b3be22b59b2ae9b7f8ea31f850dc415365d93ef7fd80b751fdc504c3d539d5ce351e3d58802adf277577a134caac20b987d629a0b3864d395'
            '9919490bbf7dba979a1df7543e62eb3fca48e8a516e6b6ab0a73080952e1b58599b7f233259d122dc66bf93f032b434d70e0dd448a1cb86513f01acb51b2120e'
            '8cafd14d414ee3c16aa94f79072bc6c100262f925dc1300e785846c3fabbbbffc1356b8e2223af5684e3340c55032d41231179ffa948bb12e01dbae0f4131911')

prepare() {
  cd "rubygems-${pkgver}"

  patch --verbose --strip=1 --input="../${pkgname}_stop_so_duplication.patch"
}

# https://github.com/rubygems/rubygems/issues/5951
#check() {
#  cd "rubygems-${pkgver}"
#
#  sed --in-place --regexp-extended 's|git ls-files|find -type f -not -path "*/\.git/*"|' Rakefile
#
#  export GEM_HOME="$(pwd)/.gem"
#  mkdir --parents --verbose "${GEM_HOME}"
#
#  rake update_manifest
#  rake test
#}

package() {
  cd "rubygems-${pkgver}"

  local _gemdir="$(ruby -e 'puts Gem.dir')"
  local _extension_api_version="$(ruby -e 'puts Gem.extension_api_version')"

  export GEM_HOME="${pkgdir}${_gemdir}"

  install --verbose -d --mode=0755 \
      "${GEM_HOME}" \
      "${pkgdir}/usr/lib/ruby/${_extension_api_version}"

  # define the current version as previous version to avoid printing the changelog
  ruby setup.rb \
    --destdir="${pkgdir}" \
    --prefix="/usr" \
    --previous-version="${pkgver}" \
    --verbose

  # fix package structure
  mv --verbose "${pkgdir}/usr/lib/${pkgname}.rb" "${pkgdir}/usr/lib/ruby/${_extension_api_version}"
  mv --verbose "${pkgdir}/usr/lib/${pkgname}" "${pkgdir}/usr/lib/ruby/${_extension_api_version}"

  # cleanup - remove any bundler references as they are provided by https://archlinux.org/packages/community/any/ruby-bundler/
  rm --verbose --recursive \
    "${pkgdir}/usr/bin/"{bundle*,rake} \
    "${pkgdir}/usr/gems" \
    "${pkgdir}/usr/lib/bundler.rb" \
    "${pkgdir}/usr/lib/bundler" \
    "${pkgdir}/usr/specifications"

  install --verbose -D --mode=0644 ../gemrc "${pkgdir}/etc/gemrc"
  install --verbose -D --mode=0644 LICENSE.txt MIT.txt --target-directory "${pkgdir}/usr/share/licenses/${pkgname}"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
